package base.http.rxandroid

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.os.Looper
import android.util.Log
import base.utils.SysUtils
import base.sys.SP_SHOPTV_INFO
import base.utils.ActivityManager
import base.utils.CustomToast
import base.utils.NetWorkUtils
import base.view.widget.RefreshDialog
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import io.reactivex.subscribers.DisposableSubscriber
import retrofit2.HttpException
import java.io.IOException
import java.net.ConnectException
import java.net.SocketException
import java.net.SocketTimeoutException


/**
 * Created by Jack on 2017/10/11.
 */

abstract class CustomDisposableSubscriber<T> : DisposableSubscriber<T> {
    private var mContext: Context? = null
    private var mGson: Gson? = null
    private val compositeDisposableUtils = CompositeDisposableUtilsV2.getInstance
    var dialogConsumer: CustomConsumer? = null

    private var refreshDialog: RefreshDialog? = null

    constructor(mContext: Context) {
        this.mContext = mContext
        mGson = Gson()
        compositeDisposableUtils.instance.addSubscription(this)
    }

    constructor(mContext: Context, dialogConsumer: CustomConsumer) {
        this.mContext = mContext
        this.dialogConsumer = dialogConsumer
        mGson = Gson()
        compositeDisposableUtils.instance.addSubscription(this)
    }

    constructor(mContext: Context, dialogConsumer: CustomConsumer, refreshDialog: RefreshDialog) {
        this.mContext = mContext
        this.dialogConsumer = dialogConsumer
        mGson = Gson()
        this.refreshDialog = refreshDialog
        compositeDisposableUtils.instance.addSubscription(this)
    }

    constructor(mContext: Context, refreshDialog: RefreshDialog) {
        this.mContext = mContext
        mGson = Gson()
        this.refreshDialog = refreshDialog
        compositeDisposableUtils.instance.addSubscription(this)
    }

//    override fun onNext(t: T) {
//        if (refreshDialog != null) {
//            refreshDialog!!.dismiss()
//        }
//        if(dialogConsumer!=null){
//            dialogConsumer!!.dismiss()
//        }
//        if (t is RespMsg<*>){
//            val respMsg = t as RespMsg<*>
//            if(respMsg.code!=0 || respMsg.code!=10066){
//                (mContext as Activity).runOnUiThread {
//                    ToastUtils.showShort(respMsg.message)
//                    LogUtil.e("onNext:" + (mContext as Activity).localClassName + ":" + GsonUtils.toJson(t))
//                }
//                if (respMsg.code == -10010 ) {
//                    LauncherApplication.setToken("")
//                    val intent = Intent(mContext, MainActivityV3::class.java)
//                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
//                    mContext!!.startActivity(intent)
//                }
//            }
//        }
//    }



    @SuppressLint("ApplySharedPref")
    override fun onError(e: Throwable) {
        try {
            if (Looper.myLooper() != Looper.getMainLooper()) return
            if (!NetWorkUtils.isNetWorkAvailable(mContext!!)){
                if (refreshDialog != null && !refreshDialog!!.isShowing) {
                    refreshDialog!!.show()
                }
            }

//                ToastUtils.makePicStringShortToast(R.drawable.warn_icon, mContext!!.getString(R.string.network_error))
            else {
                if (e is ConnectException) {
                    if (refreshDialog != null && !refreshDialog!!.isShowing) {
                        refreshDialog!!.show()
                    }
                    showToast("服务器忙")
                } else if (e is HttpException) {
                    try {
                        val errorText = e.response()!!.errorBody()?.string()
                    } catch (e: JsonSyntaxException) {
                        if (refreshDialog != null && !refreshDialog!!.isShowing) {
                            refreshDialog!!.show()
                        }
                        showToast("出错了")
                    } catch (e1: IOException) {
                        if (refreshDialog != null && !refreshDialog!!.isShowing) {
                            refreshDialog!!.show()
                        }
                        showToast("好像发生了点什么")
                    }
                } else if (e is SocketTimeoutException || e is SocketException) {
                    if (refreshDialog != null && !refreshDialog!!.isShowing) {
                        refreshDialog!!.show()
                    }
                    showToast("网络链接超时")
                } else if (e.message != null && SysUtils.isContainChinese(e.message!!)) {
                    if (refreshDialog != null && !refreshDialog!!.isShowing) {
                        refreshDialog!!.show()
                    }
                    showToast(e.message!!)
                }
            }
//            Bugtags.sendException(e)
            e.printStackTrace()
            Log.e(CustomObserver.TAG, "CustomObserver onError: ", e)
            dismiss()
        } catch (e: Exception) {
            e.printStackTrace()
            Log.e(CustomObserver.TAG, "CustomObserver onError: ", e)
        }
    }

    fun showToast(text: String) {
        CustomToast.showToast(text)
    }

    override fun onComplete() {
        dismiss()
    }

    companion object {
        private val TAG = "RX_ERROR"
    }

    fun dismiss() {
        dialogConsumer?.dismiss()
    }
}
